package com.xfgg.cloud.boot.config.redis;

import com.xfgg.cloud.boot.jedis.JedisClusterPipeLine;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashSet;
import java.util.Set;

/**
 * @author xfgg
 */
@Configuration
public class JedisConfig {
    /**
     * 初始化集群redis
     */
    public static JedisClusterPipeLine initJedisClusterPipeline(int maxActive, int maxIdle, int maxWait, String redisAdders) {

        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(maxActive);
        config.setMaxIdle(maxIdle);
        config.setMaxWaitMillis(maxWait);
        config.setTestOnBorrow(true);
        config.setMinIdle(100);

        Set<HostAndPort> clusterNodes = new HashSet<>();
        String[] hostPortArr = redisAdders.split(";");
        for (String hostPortStr : hostPortArr) {
            String[] hostPort = hostPortStr.split(":");
            String host = hostPort[0];
            int port = Integer.parseInt(hostPort[1]);
            HostAndPort hostAndPort = new HostAndPort(host, port);
            clusterNodes.add(hostAndPort);
        }
        return new JedisClusterPipeLine(clusterNodes, 1000, 2000, 3, config);

    }
}
